package cn.lu.joke.util;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.support.annotation.IntDef;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

import cn.lu.joke.App;
import cn.lu.joke.R;

public class ViewUtils {

    private ViewUtils() {}

    private static class Holder{
        private static final ViewUtils INSTANCE = new ViewUtils();
    }

    public static ViewUtils get(){
        return Holder.INSTANCE;
    }

    @SuppressWarnings("unchecked")
    public static <T extends View> T inflate(int layoutId) {
        return inflate(layoutId, App.get());
    }

    @SuppressWarnings("unchecked")
    public static <T extends View> T inflate(int layoutId, Context context) {
        return inflate(layoutId, context, null, false);
    }

    @SuppressWarnings("unchecked")
    public static <T extends View> T inflate(int layoutId, ViewGroup root) {
        return inflate(layoutId, App.get(), root, false);
    }

    @SuppressWarnings("unchecked")
    public static <T extends View> T inflate(int layoutId, ViewGroup root, boolean attach) {
        return inflate(layoutId, App.get(), root, attach);
    }

    @SuppressWarnings("unchecked")
    public static <T extends View> T inflate(int layoutId, Context context, ViewGroup root, boolean attach) {
        return (T) LayoutInflater.from(context).inflate(layoutId, root, attach);
    }

    @SuppressWarnings("unchecked")
    public static <T extends ViewDataBinding> T bindLayout(int layoutId) {
        return bindLayout(layoutId, App.get());
    }

    @SuppressWarnings("unchecked")
    public static <T extends ViewDataBinding> T bindLayout(int layoutId, ViewGroup attachToParent) {
        return bindLayout(layoutId, App.get(), attachToParent);
    }

    @SuppressWarnings("unchecked")
    public static <T extends ViewDataBinding> T bindLayout(int layoutId, Context context) {
        return (T) bindLayout(layoutId, context, null);
    }

    @SuppressWarnings("unchecked")
    public static <T extends ViewDataBinding> T bindLayout(int layoutId, Context context, ViewGroup parent) {
        return (T) DataBindingUtil.inflate(LayoutInflater.from(context), layoutId, parent, parent != null);
    }

    public static boolean setViewVisible(View view, boolean isVisible) {
        if (view != null) {
            if (isVisible) {
                if (view.getVisibility() != View.VISIBLE) {
                    view.setVisibility(View.VISIBLE);
                }
            } else {
                if (view.getVisibility() != View.GONE) {
                    view.setVisibility(View.GONE);
                }
            }
        }
        return isVisible;
    }


    public static final int DRAWABLE_LEFT = 0,
            DRAWABLE_TOP = 1,
            DRAWABLE_RIGHT = 2,
            DRAWABLE_BOTTOM = 3;

    @Retention(RetentionPolicy.SOURCE)
    @IntDef({DRAWABLE_LEFT, DRAWABLE_TOP, DRAWABLE_RIGHT, DRAWABLE_BOTTOM})
    private @interface DrawableType {
    }

    public static void addDrawable(TextView tv, int resId, int position) {
        addDrawable(tv, resId, position, 0, 0);
    }

    public static void addDrawable(TextView tv, int resId, @DrawableType int position, int w, int h) {
        Drawable drawable = tv.getContext().getResources().getDrawable(resId);
        addDrawable(tv, drawable, position, w, h);
    }

    public static void addDrawable(TextView tv, Drawable drawable, int position) {
        addDrawable(tv, drawable, position, 0, 0);
    }

    public static void addDrawable(TextView tv, Drawable drawable, int position, int w, int h) {
        Drawable[] drawables = null;
        if (drawable != null) {
            w = w > 0 ? UnitUtils.dp2px(w) : drawable.getMinimumWidth();
            h = h > 0 ? UnitUtils.dp2px(h) : drawable.getMinimumHeight();
            drawable.setBounds(0,0, w, h);

            drawables = new Drawable[4];
            for (int i = 0; i < drawables.length; i++) {
                if (i == position) {
                    drawables[i] = drawable;
                    break;
                }
            }
        }
        addDrawable(tv, drawables);
    }

    private static void addDrawable(TextView tv, Drawable[] drawArr) {
        if (drawArr == null) {
            tv.setCompoundDrawables(null, null, null, null);
        } else {
            tv.setCompoundDrawables(drawArr[0], drawArr[1], drawArr[2], drawArr[3]);
        }
    }

    public static PopupWindow getPopupWindow(View v) {
        return getPopupWindow(v, -2, -2);
    }

    public static PopupWindow getPopupWindow(View v, int width, int height) {
        PopupWindow pw = new PopupWindow(v, width, height, true);
        pw.setBackgroundDrawable(new ColorDrawable());
        pw.setOutsideTouchable(true);
        return pw;
    }
 }
